package indoor.nexd.com.nexdmap.presenter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PointF;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.nexd.location.indoor.NexdIndoorLocation;
import cn.nexd.location.indoor.NexdIndoorLocationAgent;
import cn.nexd.location.indoor.NexdIndoorLocationConfiguration;
import cn.nexd.location.indoor.NexdIndoorLocationResult;
import cn.nexd.location.indoor.listener.NexdIndoorLocationListener;
import cn.nexd.map.rendering.POIData;
import cn.nexd.map.rendering.core.componet.SVGMapView;
import cn.nexd.map.rendering.overlay.SVGMapLinesOverlay;
import cn.nexd.map.rendering.overlay.SVGMapLocationOverlay;
import indoor.nexd.com.nexdmap.R;
import indoor.nexd.com.nexdmap.application.MyApplication;
import indoor.nexd.com.nexdmap.model.FloorModel;
import indoor.nexd.com.nexdmap.model.MallModel;
import indoor.nexd.com.nexdmap.nexdmapoverly.MarkerOverlay;
import indoor.nexd.com.nexdmap.perimission.CheckPermission;
import indoor.nexd.com.nexdmap.routeplan.Dijkstra;
import indoor.nexd.com.nexdmap.routeplan.Vertex;
import indoor.nexd.com.nexdmap.ui.activity.NexdMapActivity;
import indoor.nexd.com.nexdmap.ui.inte.INexdMapActivity;
import indoor.nexd.com.nexdmap.util.DateUtil;
import indoor.nexd.com.nexdmap.util.FileUtil;
import indoor.nexd.com.nexdmap.util.ProgressDialogUtils;
import indoor.nexd.com.nexdmap.util.SplineUtil;
import indoor.nexd.com.nexdmap.util.UrlUtil;

/**
 * Created by lawrence on 2015/12/4.
 */
public class NexdMapPresenter extends BasePresenter<INexdMapActivity> {
    private final String TAG = getClass().getSimpleName();
    private FloorModel floorModel;
    private NexdIndoorLocationAgent nexdIndoorLocationAgent;
    //    private NexdLocationAgent nexdLocationAgent;
    private SVGMapView nexdMap;
    private MallModel mallModel;
    private String mallPath = ""; // 加载路径
    private SVGMapLocationOverlay svgMapLocationOverlay; // 定位图层
    private PointF locationPointF; // 路径规划起点
    private Dijkstra dijkstra;
    private SVGMapLinesOverlay linesOverlay;
    private MarkerOverlay startMarkOverlay;
    private MarkerOverlay endMarkOverlay;
    private long lastFloorId = 0;
    private List<PointF> routePlanPointFs;

    private PointF lastLocationPointF;

    private PointF locationPointf;


    private int index = 1; // 用来计数没用点的个数,成功后会重置

    private PointF lastPointf; // 上一次定位点

    private int Defaule_Distacne = 2;// 默认人行走的距离为2m
    private List<PointF> list;


    public NexdMapPresenter(Context context, INexdMapActivity iView) {
        super(context, iView);
        this.dijkstra = new Dijkstra();
    }

    public void setNexdMap(SVGMapView nexdMap) {
        this.nexdMap = nexdMap;
    }

    public void setMallModel(MallModel mallModel) {
        this.mallModel = mallModel;
        this.mallPath = UrlUtil.BASE_FILE_PATH + mallModel.getMallId() + File.separator;
    }

    public void setCurrentFloor(FloorModel floorModel) {
        this.floorModel = floorModel;
        ProgressDialogUtils.showProgressDialog(context, "切换中...", "", true);
        stopLocation();
        lastLocationPointF = null;
        String svgPath = getSvgPath(floorModel.getFloorSvg());
        Log.d("切换楼层", svgPath);
        try {
            dijkstra.load_config(UrlUtil.BASE_FILE_PATH + mallModel.getMallId() + "/" + floorModel.getFloorId() + ".road");
        } catch (IOException e) {
            MyApplication.getInstance().reportException(e);
        }
        if (checkFileExist(svgPath)) {
            iView.setCurrentFloor(svgPath);
        } else {
            iView.showToast("未找到当前楼层信息");
        }
    }

    public void loadMap(FloorModel floorModel) {
        this.floorModel = floorModel;
        // 加载当前楼层的路网数据
        if (MyApplication.IS_ROUTE_PLAN) {
            try {
                dijkstra.load_config(UrlUtil.BASE_FILE_PATH + mallModel.getMallId() + "/" + floorModel.getFloorId() + ".road");
            } catch (Exception e) {
                MyApplication.getInstance().reportException(e);
            }
        }
        Log.d("svgPath", getSvgPath(floorModel.getFloorSvg()));
        if (!CheckPermission.checkPermission((Activity) context, "android.permission.WRITE_EXTERNAL_STORAGE")
                || !CheckPermission.checkPermission((Activity) context, "android.permission.READ_EXTERNAL_STORAGE")) {
            iView.showToast("存储权限被拒绝,请在设置中允许");
            return;
        }
        if (checkFileExist(getSvgPath(floorModel.getFloorSvg()))) {
            iView.loadMap(getSvgPath(floorModel.getFloorSvg()));
        } else {
            iView.showToast("未找到当前楼层信息");
        }
    }

    public void saveNexdMap(Bitmap bitmap) {
        if (!CheckPermission.checkPermission((Activity) context, "android.permission.WRITE_EXTERNAL_STORAGE")
                || !CheckPermission.checkPermission((Activity) context, "android.permission.READ_EXTERNAL_STORAGE")) {
            iView.showToast("存储权限被拒绝,请在设置中允许");
            return;
        }
        // 当前地图截图
        String fileName = UrlUtil.BASE_FILE_PATH + File.separator + "img" + File.separator + DateUtil.getCurrentByType("yyyyMMDDHHmmss") + ".png";
        File file = FileUtil.buildFile(fileName, false);
        try {
            FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
        } catch (IOException e) {
            MyApplication.getInstance().reportException(e);
        }
    }


    public void routePlanPOI2POI(POIData start, POIData end) {
        if (dijkstra == null) {
            iView.showToast(context.getString(R.string.dijkstra_data_null));
            return;
        }
        List<Vertex> points;
        points = dijkstra.get_path_poi_poi(start.getPoi_id(), end.getPoi_id());

        if (points != null && points.size() == 0) {
            iView.showToast("无法规划路线到目的地");
            return;
        }
        if (startMarkOverlay == null) {
            startMarkOverlay = new MarkerOverlay(context);
        }

        PointF currentPointF = new PointF(start.getCenter_x(), start.getCenter_y());
        startMarkOverlay.setPosition(currentPointF);
        startMarkOverlay.setRes(R.drawable.icon_st);
        iView.addOverlay(startMarkOverlay, NexdMapActivity.MARKER);

        if (endMarkOverlay == null) {
            endMarkOverlay = new MarkerOverlay(context);
        }
        endMarkOverlay.setPosition(new PointF(end.getCenter_x(), end.getCenter_y()));
        endMarkOverlay.setRes(R.drawable.icon_en);
        iView.addOverlay(endMarkOverlay, NexdMapActivity.MARKER);

        if (linesOverlay == null) {
            linesOverlay = new SVGMapLinesOverlay(nexdMap);
        }
        // 开始规划路线
        if (routePlanPointFs == null) {
            routePlanPointFs = new ArrayList<>();
        } else {
            routePlanPointFs.clear();
        }
        routePlanPointFs.add(currentPointF);

        if (points == null) {
            iView.showToast("无法规划目标路线,请重新选择");
            return;
        }

        for (Vertex vertex : points) {
            routePlanPointFs.add(new PointF((float) vertex.location[0], (float) vertex.location[1]));

        }
        linesOverlay.setPositions(routePlanPointFs);
        iView.addOverlay(linesOverlay, NexdMapActivity.MARKER);

        iView.resetRoutePlan();


    }

    public void routePlanXY2POI(POIData poiData) {

        if (locationPointF == null) {
            iView.showToast("定位中...");
            return;
        }

        if (dijkstra == null) {
            iView.showToast(context.getString(R.string.dijkstra_data_null));
            return;
        }
        // 路径规划计算
        PointF currentLocation = getCurrentLocationPointF();
        List<Vertex> points;
        try {
            points = dijkstra.get_path_poi(
                    new double[]{currentLocation.x, currentLocation.y},
                    poiData.getPoi_id(),
                    poiData.getFloor_id());

        } catch (ArrayIndexOutOfBoundsException e) {
            iView.showToast("无法规划路线到目的地");
            MyApplication.getInstance().reportException(e);
            e.printStackTrace();
            return;
        }

        // 判断是否可以到达指定地点
        if (points != null && points.size() == 0) {
            iView.showToast("无法规划路线到目的地");
            return;
        }

        if (startMarkOverlay == null) {
            startMarkOverlay = new MarkerOverlay(context);
        }

        startMarkOverlay.setPosition(currentLocation);
        startMarkOverlay.setRes(R.drawable.icon_st);
        iView.addOverlay(startMarkOverlay, NexdMapActivity.MARKER);

        if (endMarkOverlay == null) {
            endMarkOverlay = new MarkerOverlay(context);
        }
        endMarkOverlay.setPosition(new PointF(poiData.getCenter_x(), poiData.getCenter_y()));
        endMarkOverlay.setRes(R.drawable.icon_en);
        iView.addOverlay(endMarkOverlay, NexdMapActivity.MARKER);

        if (linesOverlay == null) {
            linesOverlay = new SVGMapLinesOverlay(nexdMap);
        }
        // 开始规划路线
        if (routePlanPointFs == null) {
            routePlanPointFs = new ArrayList<>();
        } else {
            routePlanPointFs.clear();
        }
        routePlanPointFs.add(currentLocation);
        assert points != null;


        for (Vertex vertex : points) {
            routePlanPointFs.add(new PointF((float) vertex.location[0], (float) vertex.location[1]));
        }
        linesOverlay.setPositions(routePlanPointFs);
        iView.addOverlay(linesOverlay, NexdMapActivity.MARKER);
        iView.resetRoutePlan();

    }


    private static float DISTANCE = 0.5f;

    private PointF recala(Vertex last, Vertex current, Vertex next) {


        if (last == null || next == null) {
            return new PointF((float) current.location[0], (float) current.location[1]);
        } else {
            // 先判断上一个点和当前这个点是否在一条线上,如果在, 判断下一个点是否转弯
            // 当前点和上一个点在一条线上,并且下一个点没有转弯, 不画当前点
            // 当前点和上一个点在一条线上,并且下一个点转弯,画当前点

            // 当前点和上一个点不在一条线上,画
            if (((last.location[1] - current.location[1])
                    / (last.location[0] - current.location[0]))
                    * ((current.location[1] - next.location[1])
                    / (current.location[0] - next.location[0]))
                    > 0 && ((last.location[1] - current.location[1])
                    / (last.location[0] - current.location[0]))
                    * ((current.location[1] - next.location[1])
                    / (current.location[0] - next.location[0])) < 1) {
                return new PointF((float) current.location[0], (float) current.location[1]);
            } else {
                return null;
//                return new PointF((float) current.location[0], (float) current.location[1]);
            }
        }
    }


    public long getCurrentFloorId() {
        return Long.parseLong(this.floorModel.getFloorId());
    }

    public void destory() {
        nexdIndoorLocationAgent.destroyLocation();
//        nexdLocationAgent.destroy();
    }

    public PointF getCurrentLocationPointF() {

        return new PointF(locationPointF.x, locationPointF.y);
    }

    /*
     * 检查文件是否存在， 用来判断是否确实文件
     */
    private boolean checkFileExist(String svgPath) {
        if (FileUtil.checkExist(svgPath)) {
            return true;
        } else {
            return false;
        }
    }

    // 获取svg文件路径
    private String getSvgPath(String svgName) {
        return mallPath + svgName;
    }
//
//    private StringBuilder sb = new StringBuilder();
//    private int index = 0;
//    private long currentTime = System.currentTimeMillis();

    public void initMap() {

        nexdIndoorLocationAgent = NexdIndoorLocationAgent.getIndoorLocationAgent(context);
        NexdIndoorLocationConfiguration.Buidler config = new NexdIndoorLocationConfiguration.Buidler();
        config.setAppkey("");
        config.setLocationRate(MyApplication.LOCATION_TIME * 1000); // 定位时间间隔
        if (MyApplication.IS_BEACON && MyApplication.IS_WIFI) {
            config.setLocationMode(NexdIndoorLocationConfiguration.LocationMode.LOCATION_MODE_MIX);
            Log.d(TAG, "initMap: Mix Mode");
        } else if (MyApplication.IS_WIFI) {
            config.setLocationMode(NexdIndoorLocationConfiguration.LocationMode.LOCATION_MODE_WIFI_ONLY);
            Log.d(TAG, "initMap: Wifi Mode");
        } else if (MyApplication.IS_BEACON) {
            config.setLocationMode(NexdIndoorLocationConfiguration.LocationMode.LOCATION_MODE_BEACON_ONLY);
            Log.d(TAG, "initMap: Beacon Mode");
        }
//        config.setLocationConfigPath("/sdcard/nexd/map/");
        nexdIndoorLocationAgent.setIndoorLocationConfiguration(config.build());


        nexdIndoorLocationAgent.setIndoorLocationListener(new NexdIndoorLocationListener() {
            @Override
            public void onLocationStart(long l, long l1, NexdIndoorLocationConfiguration.LocationMode locationMode) {

            }

            @Override
            public void onLocationSuccess(NexdIndoorLocationResult nexdIndoorLocationResult) {

                // TODO: 16/6/29  滑动显示坐标,以及防止坐标大跳的算法逻辑
                // 滑动算法
//                List<PointF> pointFs = recalas(new PointF(nexdIndoorLocationResult.getLocationResultX(), nexdIndoorLocationResult.getLocationResultY()));
//                PointF current = recalas(new PointF(nexdIndoorLocationResult.getLocationResultX(), nexdIndoorLocationResult.getLocationResultY()));
//                try {
////                    for (PointF current : pointFs) {
//                    if (MyApplication.IS_SMOOTH_OUTPUT) {
//                        Log.d("LocationListener", "进入平滑输出");
////                             平滑输出
//                        if (svgMapLocationOverlay == null) {
//                            Log.d("svgMapLocationOverlay", "我不是空!");
//                            svgMapLocationOverlay = new SVGMapLocationOverlay(nexdMap);
//                        }
//                        if (nexdMap.isMapLoadFinsh()) {
//                            Log.i("LocationOverlay", "add overlay");
//                            Log.d("locationPoints", current.x + ",,,," + current.y);
//                            PointF currentLocationPointF;
//                            if (lastLocationPointF != null) {
//                                currentLocationPointF = SplineUtil.getPoint(lastLocationPointF, current);
//                            } else {
//                                currentLocationPointF = current;
//                            }
//                            if (locationPointF == null) {
//                                locationPointF = new PointF(currentLocationPointF.x - nexdMap.getFloorFeature().getOffset_x(), currentLocationPointF.y - nexdMap.getFloorFeature().getOffset_y());
//                            } else {
//                                locationPointF = new PointF(currentLocationPointF.x - nexdMap.getFloorFeature().getOffset_x(), currentLocationPointF.y - nexdMap.getFloorFeature().getOffset_y());
//                            }
//                            lastLocationPointF = currentLocationPointF;
////                                当前定位点
//                            svgMapLocationOverlay.setPosition(currentLocationPointF);
//                            iView.addOverlay(svgMapLocationOverlay, NexdMapActivity.LOCATION);
//                            NexdMapActivity.IS_LOCATIONING = true;
//                        }
//                    } else {
////                             正常定位
//                        Log.d("LocationListener", "进入非平滑输出");
//                        if (svgMapLocationOverlay == null) {
//                            Log.d("svgMapLocationOverlay", "我不是空!");
//                            svgMapLocationOverlay = new SVGMapLocationOverlay(nexdMap);
//                        }
//                        if (nexdMap.isMapLoadFinsh()) {
//                            Log.i("LocationOverlay", "add overlay");
//                            Log.d("locationPoints", current.x + ",,,," + current.y);
//                            if (locationPointF == null) {
//                                locationPointF = new PointF(current.x - nexdMap.getFloorFeature().getOffset_x(), current.y - nexdMap.getFloorFeature().getOffset_y());
//                            } else {
//                                locationPointF.set(current.x - nexdMap.getFloorFeature().getOffset_x(), current.y - nexdMap.getFloorFeature().getOffset_y());
//                            }
////                                当前定位点
//                            svgMapLocationOverlay.setPosition(current);
//                            iView.addOverlay(svgMapLocationOverlay, NexdMapActivity.LOCATION);
//                            NexdMapActivity.IS_LOCATIONING = true;
//                        }
////                    }
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }


                // // TODO: 16/6/28  edit by tony   原始定位显示逻辑
                if (MyApplication.IS_SMOOTH_OUTPUT) {
                    Log.d("LocationListener", "进入平滑输出");
                    // 平滑输出
                    if (svgMapLocationOverlay == null) {
                        Log.d("svgMapLocationOverlay", "我不是空!");
                        svgMapLocationOverlay = new SVGMapLocationOverlay(nexdMap);
                    }
                    if (nexdMap.isMapLoadFinsh()) {
                        Log.i("LocationOverlay", "add overlay");
                        Log.d("locationPoints", nexdIndoorLocationResult.getLocationResultX() + ",,,," + nexdIndoorLocationResult.getLocationResultY());
                        PointF currentLocationPointF;
                        if (lastLocationPointF != null) {
                            currentLocationPointF = SplineUtil.getPoint(lastLocationPointF, new PointF(nexdIndoorLocationResult.getLocationResultX(), nexdIndoorLocationResult.getLocationResultY()));
                        } else {
                            currentLocationPointF = new PointF(nexdIndoorLocationResult.getLocationResultX(), nexdIndoorLocationResult.getLocationResultY());
                        }
                        if (locationPointF == null) {
                            locationPointF = new PointF(currentLocationPointF.x - nexdMap.getFloorFeature().getOffset_x(), currentLocationPointF.y - nexdMap.getFloorFeature().getOffset_y());
                        } else {
                            locationPointF = new PointF(currentLocationPointF.x - nexdMap.getFloorFeature().getOffset_x(), currentLocationPointF.y - nexdMap.getFloorFeature().getOffset_y());
                        }
                        lastLocationPointF = currentLocationPointF;
                        //当前定位点
                        svgMapLocationOverlay.setPosition(currentLocationPointF);
                        iView.addOverlay(svgMapLocationOverlay, NexdMapActivity.LOCATION);
                        NexdMapActivity.IS_LOCATIONING = true;
                    }
                } else {
                    // 正常定位
                    Log.d("LocationListener", "进入非平滑输出");
                    if (svgMapLocationOverlay == null) {
                        Log.d("svgMapLocationOverlay", "我不是空!");
                        svgMapLocationOverlay = new SVGMapLocationOverlay(nexdMap);
                    }
                    if (nexdMap.isMapLoadFinsh()) {
                        Log.i("LocationOverlay", "add overlay");
                        Log.d("locationPoints", nexdIndoorLocationResult.getLocationResultX() + ",,,," + nexdIndoorLocationResult.getLocationResultY());
                        if (locationPointF == null) {
                            locationPointF = new PointF(nexdIndoorLocationResult.getLocationResultX() - nexdMap.getFloorFeature().getOffset_x(), nexdIndoorLocationResult.getLocationResultY() - nexdMap.getFloorFeature().getOffset_y());
                        } else {
                            locationPointF.set(nexdIndoorLocationResult.getLocationResultX() - nexdMap.getFloorFeature().getOffset_x(), nexdIndoorLocationResult.getLocationResultY() - nexdMap.getFloorFeature().getOffset_y());
                        }
                        //当前定位点
                        svgMapLocationOverlay.setPosition(new PointF(nexdIndoorLocationResult.getLocationResultX(), nexdIndoorLocationResult.getLocationResultY()));
                        iView.addOverlay(svgMapLocationOverlay, NexdMapActivity.LOCATION);
                        NexdMapActivity.IS_LOCATIONING = true;
                    }
                }
            }

            @Override
            public void onLocationFailed(NexdIndoorLocation.LocationState locationState) {
                Log.e(TAG, "onLocationFailed: " + locationState.toString());
            }

            @Override
            public void onLocationInterrupt(NexdIndoorLocation.LocationState locationState) {
                Log.e(TAG, "onLocationInterrupt: " + locationState.toString());

            }
        });
//        nexdLocationAgent = NexdLocationAgent.getLocationAgent();
//        nexdLocationAgent.setContext(context);
//        NexdLocationConfig.Buidler config = new NexdLocationConfig.Buidler();
//        config.setAppkey("");
//        // 定位模式
//        if (MyApplication.IS_BEACON && MyApplication.IS_WIFI) {
//            config.setLocationMode(NexdLocationConfig.LocationMode.MIX);
//            Log.d("Location Mode", "MIX Mode");
//        } else if (MyApplication.IS_WIFI) {
//            config.setLocationMode(NexdLocationConfig.LocationMode.WIFI_ONLY);
//            Log.d("Location Mode", "WIFI_ONLY Mode");
//        } else if (MyApplication.IS_BEACON) {
//            config.setLocationMode(NexdLocationConfig.LocationMode.BLUETOOTH_ONLY);
//            Log.d("Location Mode", "BLUETOOTH_ONLY Mode");
//        }
//        Log.d("LocationListener", MyApplication.LOCATION_TIME + "");
//        config.setLocationRate(MyApplication.LOCATION_TIME * 1000); // 定位频率为1000ms
//        nexdLocationAgent.setLocationConfig(config.build());
//        nexdLocationAgent.setBluetoothCollectorListener(new Collector.BluetoothCollectorListener() {
//            @Override
//            public void onFailed(long l, long l1, int i, String s) {
//
//            }
//
//            @Override
//            public void onStop(long l, long l1, int i, String s) {
//
//            }
//
//            @Override
//            public void onSuccess(long l, long l1, List<BluetoothCollectorResult> list) {
//                sb.append(index);
//                for (BluetoothCollectorResult bluetoothCollectorResult : list) {
//                    sb.append(" ")
//                            .append(bluetoothCollectorResult.getAddress())
//                            .append(" ")
//                            .append(bluetoothCollectorResult.getRssi());
//                }
//                sb.append("\n");
//                try {
//                    Log.d(TAG, "onSuccess: " + sb.toString());
//                    FileUtil.writeFile(UrlUtil.BASE_FILE_PATH + File.separator + "collect" + File.separator + currentTime + ".blue", new String(sb.toString()));
//                    sb.setLength(0);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//                index++;
//            }
//        });
        // 定位结果监听, 定位成功,添加定位涂层
//        nexdLocationAgent.setLocationListener(new NexdLocationListener() {
//            @Override
//            public void onLocationStart() {
//                Log.d("LocationListener", "开始定位");
//            }
//
//            @Override
//            public void onLocationSuccess(long l, LocationResult locationResult) {
//                if (lastFloorId != 0) {
//                    if (lastFloorId != locationResult.getLocationResultFloorId()) {
//                        lastFloorId = locationResult.getLocationResultFloorId();
//                        if (nexdMap.isMapLoadFinsh()) {
////                            iView.showToast("您已不在当前楼层，请切换" + lastFloorId);
////                            iView.showToast("您已不在当前楼层，请切换到相应楼层");
//                        }
//                    }
//                } else {
//                    lastFloorId = locationResult.getLocationResultFloorId();
//                }
//
//                if (lastPointf == null) {
//                    lastPointf = new PointF(locationResult.getLocationResultX(), locationResult.getLocationResultY());
//                }
//
//                // TODO: 16/6/28  edit by tony 防止大跳算法调用 外加 滑动显示定位点逻辑
////                List<PointF> pointFs = recalas(new PointF(locationResult.getLocationResultX(), locationResult.getLocationResultY()));
////                PointF current = recalas(new PointF(locationResult.getLocationResultX(), locationResult.getLocationResultY()));
////                try {
////                    for (PointF current : pointFs) {
////                        if (MyApplication.IS_SMOOTH_OUTPUT) {
////                            Log.d("LocationListener", "进入平滑输出");
////                             平滑输出
////                            if (svgMapLocationOverlay == null) {
////                                Log.d("svgMapLocationOverlay", "我不是空!");
////                                svgMapLocationOverlay = new SVGMapLocationOverlay(nexdMap);
////                            }
////                            if (nexdMap.isMapLoadFinsh()) {
////                                Log.i("LocationOverlay", "add overlay");
////                                Log.d("locationPoints", current.x + ",,,," + current.y);
////                                PointF currentLocationPointF;
////                                if (lastLocationPointF != null) {
////                                    currentLocationPointF = SplineUtil.getPoint(lastLocationPointF, current);
////                                } else {
////                                    currentLocationPointF = current;
////                                }
////                                if (locationPointF == null) {
////                                    locationPointF = new PointF(currentLocationPointF.x - nexdMap.getFloorFeature().getOffset_x(), currentLocationPointF.y - nexdMap.getFloorFeature().getOffset_y());
////                                } else {
////                                    locationPointF = new PointF(currentLocationPointF.x - nexdMap.getFloorFeature().getOffset_x(), currentLocationPointF.y - nexdMap.getFloorFeature().getOffset_y());
////                                }
////                                lastLocationPointF = currentLocationPointF;
////                                当前定位点
////                                svgMapLocationOverlay.setPosition(currentLocationPointF);
////                                iView.addOverlay(svgMapLocationOverlay, NexdMapActivity.LOCATION);
////                                NexdMapActivity.IS_LOCATIONING = true;
////                            }
////                        } else {
////                             正常定位
////                            Log.d("LocationListener", "进入非平滑输出");
////                            if (svgMapLocationOverlay == null) {
////                                Log.d("svgMapLocationOverlay", "我不是空!");
////                                svgMapLocationOverlay = new SVGMapLocationOverlay(nexdMap);
////                            }
////                            if (nexdMap.isMapLoadFinsh()) {
////                                Log.i("LocationOverlay", "add overlay");
////                                Log.d("locationPoints", current.x + ",,,," + current.y);
////                                if (locationPointF == null) {
////                                    locationPointF = new PointF(current.x - nexdMap.getFloorFeature().getOffset_x(), current.y - nexdMap.getFloorFeature().getOffset_y());
////                                } else {
////                                    locationPointF.set(current.x - nexdMap.getFloorFeature().getOffset_x(), current.y - nexdMap.getFloorFeature().getOffset_y());
////                                }
////                                当前定位点
////                                svgMapLocationOverlay.setPosition(current);
////                                iView.addOverlay(svgMapLocationOverlay, NexdMapActivity.LOCATION);
////                                NexdMapActivity.IS_LOCATIONING = true;
////                            }
////                        }
////                    }
////                    pointFs.clear();
////                } catch (Exception e) {
////                    e.printStackTrace();
////                }
//
//
//                // // TODO: 16/6/28  edit by tony   原始定位显示逻辑
//                if (MyApplication.IS_SMOOTH_OUTPUT) {
//                    Log.d("LocationListener", "进入平滑输出");
//                    // 平滑输出
//                    if (svgMapLocationOverlay == null) {
//                        Log.d("svgMapLocationOverlay", "我不是空!");
//                        svgMapLocationOverlay = new SVGMapLocationOverlay(nexdMap);
//                    }
//                    if (nexdMap.isMapLoadFinsh()) {
//                        Log.i("LocationOverlay", "add overlay");
//                        Log.d("locationPoints", locationResult.getLocationResultX() + ",,,," + locationResult.getLocationResultY());
//                        PointF currentLocationPointF;
//                        if (lastLocationPointF != null) {
//                            currentLocationPointF = SplineUtil.getPoint(lastLocationPointF, new PointF(locationResult.getLocationResultX(), locationResult.getLocationResultY()));
//                        } else {
//                            currentLocationPointF = new PointF(locationResult.getLocationResultX(), locationResult.getLocationResultY());
//                        }
//                        if (locationPointF == null) {
//                            locationPointF = new PointF(currentLocationPointF.x - nexdMap.getFloorFeature().getOffset_x(), currentLocationPointF.y - nexdMap.getFloorFeature().getOffset_y());
//                        } else {
//                            locationPointF = new PointF(currentLocationPointF.x - nexdMap.getFloorFeature().getOffset_x(), currentLocationPointF.y - nexdMap.getFloorFeature().getOffset_y());
//                        }
//                        lastLocationPointF = currentLocationPointF;
//                        //当前定位点
//                        svgMapLocationOverlay.setPosition(currentLocationPointF);
//                        iView.addOverlay(svgMapLocationOverlay, NexdMapActivity.LOCATION);
//                        NexdMapActivity.IS_LOCATIONING = true;
//                    }
//                } else {
//                    // 正常定位
//                    Log.d("LocationListener", "进入非平滑输出");
//                    if (svgMapLocationOverlay == null) {
//                        Log.d("svgMapLocationOverlay", "我不是空!");
//                        svgMapLocationOverlay = new SVGMapLocationOverlay(nexdMap);
//                    }
//                    if (nexdMap.isMapLoadFinsh()) {
//                        Log.i("LocationOverlay", "add overlay");
//                        Log.d("locationPoints", locationResult.getLocationResultX() + ",,,," + locationResult.getLocationResultY());
//                        if (locationPointF == null) {
//                            locationPointF = new PointF(locationResult.getLocationResultX() - nexdMap.getFloorFeature().getOffset_x(), locationResult.getLocationResultY() - nexdMap.getFloorFeature().getOffset_y());
//                        } else {
//                            locationPointF.set(locationResult.getLocationResultX() - nexdMap.getFloorFeature().getOffset_x(), locationResult.getLocationResultY() - nexdMap.getFloorFeature().getOffset_y());
//                        }
//                        //当前定位点
//                        svgMapLocationOverlay.setPosition(new PointF(locationResult.getLocationResultX(), locationResult.getLocationResultY()));
//                        iView.addOverlay(svgMapLocationOverlay, NexdMapActivity.LOCATION);
//                        NexdMapActivity.IS_LOCATIONING = true;
//                    }
//                }
//
//
//            }
//
//            @Override
//            public void onLocationFailed(int i, String s) {
//                Log.d("LocationListener", "定位失败");
//            }
//
//            @Override
//            public void onLocationInterrupt(int i, String s) {
//                Log.d("LocationListener", "定位中断");
//            }
//        });
    }


    private PointF recalas(PointF currentPointf) {
        if (lastPointf == null) {
            lastPointf = currentPointf;
            List<PointF> list = new ArrayList<>();
            list.add(currentPointf);
//            return list;
            return currentPointf;
        } else {

//            if (list == null) {
//                list = new ArrayList<>();
//            }
            double distance = Math.sqrt(Math.pow(lastPointf.x - currentPointf.x, 2) + Math.pow(lastPointf.y - currentPointf.y, 2));

            if (distance > (index * Defaule_Distacne)) {
                Log.d(TAG, "recalas: " + distance + "   " + index);
                index++;
                if (index == 5) {
//                    list.clear();
//                    list.addAll(SplineUtil.spline(new float[]{lastPointf.x, lastPointf.y}, new float[]{currentPointf.x, currentPointf.y}, 1.5d));
                    lastPointf = currentPointf;
                    index = 1;
                }
                return lastPointf;
            } else {
//                list.clear();
//                list.addAll(SplineUtil.spline(new float[]{lastPointf.x, lastPointf.y}, new float[]{currentPointf.x, currentPointf.y}, 1.5d));
                lastPointf = currentPointf;
                index = 1;
                return currentPointf;
            }
        }
    }

    public void startLocation() {
        if (MyApplication.AUTO_CHANGE_FLOOR) {
            nexdIndoorLocationAgent.startLocation(Long.parseLong(mallModel.getMallId()));
//            nexdLocationAgent.startLocation(Long.parseLong(mallModel.getMallId()));
            Log.d(TAG, "startLocation: Auto_Change_Floor");
        } else {
            nexdIndoorLocationAgent.startLocation(Long.parseLong(mallModel.getMallId()), Long.parseLong(floorModel.getFloorId()));
//            nexdLocationAgent.startLocation(Long.parseLong(mallModel.getMallId()), Long.parseLong(floorModel.getFloorId()));
            Log.d(TAG, "startLocation: Not Auto_Change_Floor");
        }
    }

    public void stopLocation() {

        nexdIndoorLocationAgent.stopLocation();
        Log.d(TAG, "stopLocation: 定位停止");
//        nexdLocationAgent.stopLocation();

    }
}
